package com.wjk.cms.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class UploadUtil {

    @Value("${app.upload-folder-path}")
    private String uploadFolderPath;

    private String getImgsFolderPath() {
        return uploadFolderPath + File.separator + "upload" + File.separator + "imgs";
    }

    private String getImgsFolderRelativePath() {
        String[] parts = uploadFolderPath.split(System.getProperty("os.name").toLowerCase().contains("windows") ? "\\\\" : File.separator);
        return parts[parts.length - 1] + File.separator + "upload" + File.separator + "imgs";
    }

    public File getUploadFolder() {
        String imgsFolderPath = getImgsFolderPath();
        // 构建上传文件的存放 "文件夹" 路径
        File folder = new File(imgsFolderPath);
        if (!folder.exists()) {
            folder.mkdirs(); // 递归生成文件夹
        }
        return folder;
    }

    public String upload(MultipartFile file, String username, String category) {
        Map<String, Object> msg = new HashMap<>();
        log.info("username:"+username+"category:"+category);
        if (file.isEmpty()) {
            log.info("file 为空！");
            return null;
        }
        String filename = username + "-" + category + "-" + CommonUtil.getTimeStamp() + "-" + file.getOriginalFilename();
        File uploadFolder = getUploadFolder();
        try {
            String path = uploadFolder.getAbsolutePath() + File.separator + filename;
            File newFile = new File(path);
            log.info("上传: " + newFile.getAbsolutePath());
            file.transferTo(newFile);
            return getImgsFolderRelativePath() + File.separator + newFile.getName();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public String uploadBk(MultipartFile file, String username, String category) {
        Map<String, Object> msg = new HashMap<>();
        log.info("username:"+username+"category:"+category);
        if (file.isEmpty()) {
            log.info("file 为空！");
            return null;
        }
        String filename = username + "-" + category + "-" + CommonUtil.getTimeStamp() + "-" + file.getOriginalFilename();
        String imgsFolderPath = "/www/wwwroot/scm/bk" + File.separator + "uploadFile" + File.separator + "imgs";
        // 构建上传文件的存放 "文件夹" 路径
        File uploadFolder = new File(imgsFolderPath);
        if (!uploadFolder.exists()) {
            uploadFolder.mkdirs(); // 递归生成文件夹
        }
        try {
            String path = uploadFolder.getAbsolutePath() + File.separator + filename;
            File newFile = new File(path);
            log.info("上传: " + newFile.getAbsolutePath());
            file.transferTo(newFile);
            String[] parts = "/www/wwwroot/scm/bk".split(System.getProperty("os.name").toLowerCase().contains("windows") ? "\\\\" : File.separator);
            log.info(parts.toString());
            return "/uploadFile/imgs" + File.separator + newFile.getName();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

}
